// pages/mechAllcour/mechAllcour.js
var app=getApp();
var util=require("../../utils/util");
var home=require("../../api/home");
var group=require("../../api/group");

Page({
  /*页面的初始数据*/
  data: {
    id: '',
    type: '',
    cid: '',
    step: '',
    com_data: [],
    show_address: false,
    currentPage: 1,
    pageSize: 10,
    hasMoreDate: true,
    latitude: 22.80536,
    longitude: 113.29321,
    show_nav: false
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this,id=options.id,type=options.type,cid=options.cid,step=options.step,title="全部课程";
    that.setData({id:id,type:type,cid:cid,step:step})
    if(type==1){
      if(step==1){
        if(cid==0){
          title="正价课程"
        }else{
          title="体验课程"
        }
      }
    }else{
      title="拼团课程"
    }
    wx.setNavigationBarTitle({
      title: title,
    })
    wx.getStorage({
      key: 'city',
      success:function(res){
        var info=res.data;
        that.setData({latitude: info.lat,longitude: info.lng});
        if(type==1){
          that.get_cour_list(id,cid,step);
        }else{
          that.get_group_list(id);
        }
      }
    })
  },
  // 空数据
  nav_home:function(){
    wx.switchTab({
      url: '../home/home',
    })
  },
  // 触底
  onReachBottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,id=that.data.id,type=that.data.type,cid=that.data.cid,step=that.data.step
    if(hasMoreData){
      if(type==1){
        that.get_cour_list(id,cid,step);
      }else{
        that.get_group_list(id);
      }
      
    }else{
      util.page_show_toast('没有更多了',1500);
    }
  },
  // 获取机构课程列表
  get_cour_list:function(bid,cid,step){
    wx.showLoading({title: '加载中'})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "status": 3,
      "businessId": bid,
      "courseNatureId": cid,
      "lat": that.data.latitude,
      "lng": that.data.longitude
    }
    if(step==2){delete params.courseNatureId}
    home.get_cour_list(params).then(res=>{
      if(res.data.code==200){
        var list=res.data.data.records;
        if(list.length>0){
          for(var i=0;i<list.length;i++){
            list[i].introducePictures=list[i].introducePictures?list[i].introducePictures.split(","):'';
            list[i].score=list[i].score?list[i].score.toFixed(1):'5.0';
            list[i].space=list[i].distance>1000?(list[i].distance/1000).toFixed(2):list[i].distance;
            list[i].labelName=list[i].labelName?list[i].labelName.split(","):'';
            list[i].courseNatureName=list[i].courseNatureName?list[i].courseNatureName.substr(0,2):''
            list[i].sellCount=list[i].sellCount?list[i].sellCount:0
          }
        }
        var com_data = that.data.com_data;
        com_data = util.extend(com_data, list)
        if (list.length < pageSize) {
          that.setData({
            com_data: com_data,
            hasMoreData: false
          })
        } else {
          that.setData({
            com_data: com_data,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
        if(list.length==0&&com_data.length==0){
          that.setData({show_nav:true})
        }
      }
    })
  },
  // 拼团课程列表
  get_group_list:function(id){
    wx.showLoading({title:"加载中"})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "activityType": 1,
      "name": '',
      "status": 2,
      "delFlag": 0,
      "upDownShelves": 1,
      "businessId": id,
      "lat": that.data.latitude,
      "lng": that.data.longitude
    }
    group.get_group_list(params).then(res=>{
      if(res.data.code==200){
        var list=res.data.data.records;
        for(var i=0;i<list.length;i++){
          list[i].introducePictures=list[i].courseAvatar?list[i].courseAvatar.split(","):'';
          list[i].score=list[i].score?list[i].score.toFixed(1):'5.0';
          list[i].space=list[i].distance>1000?(list[i].distance/1000).toFixed(2):list[i].distance;
        }
        var com_data = that.data.com_data;
        com_data = util.extend(com_data, list)
        if (list.length < pageSize) {
          that.setData({
            com_data: com_data,
            hasMoreData: false
          })
        } else {
          that.setData({
            com_data: com_data,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
        if(list.length==0&&com_data.length==0){
          that.setData({show_nav:true})
        }
      }
    })
  },
})